package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.PlatOrganization;
import com.hoshiicloud.user.param.organization.PlatOrganizationAddParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationQueryParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationUpdateParam;
import com.hoshiicloud.user.service.organization.PlatOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 机构表 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Slf4j
@RestController
@RequestMapping("/organization/plat-organization")
@Api(value = "租户平台机构", tags = {"租户平台机构"})
@ApiSort(20)
public class PlatOrganizationController {
    @Autowired
    private PlatOrganizationService platOrganizationService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询机构表
     */
    @ApiOperation(value = "分页查询平台机构表")
    @PostMapping(value = "/getPage")
    public Response<IPage<PlatOrganization>> getPage(@RequestBody PlatOrganizationQueryParam param) {
        try {
            QueryWrapper<PlatOrganization> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getPlatId() != null && param.getPlatId() != 0), PlatOrganization::getPlatId, param.getPlatId())
                    .eq((param.getOrganizationTypeId() != null), PlatOrganization::getOrganizationTypeId, param.getOrganizationTypeId())
                    .eq((param.getType() != null), PlatOrganization::getType, param.getType())
                    .eq((param.getParentId() != null && param.getParentId() != 0), PlatOrganization::getParentId, param.getParentId())
                    .eq(StringUtils.isNotBlank(param.getName()), PlatOrganization::getName, param.getName())
                    .eq(StringUtils.isNotBlank(param.getCharge()), PlatOrganization::getCharge, param.getCharge())
                    .eq(StringUtils.isNotBlank(param.getIdCard()), PlatOrganization::getIdCard, param.getIdCard())
                    .eq((param.getAccountType() != null), PlatOrganization::getType, param.getType())
                    .eq(StringUtils.isNotBlank(param.getAccountName()), PlatOrganization::getAccountName, param.getAccountName())
                    .eq(StringUtils.isNotBlank(param.getBankNumber()), PlatOrganization::getBankNumber, param.getBankNumber())
                    .eq(StringUtils.isNotBlank(param.getBankName()), PlatOrganization::getBankName, param.getBankName())
                    .eq(StringUtils.isNotBlank(param.getBankAddress()), PlatOrganization::getBankAddress, param.getBankAddress())
                    .eq(StringUtils.isNotBlank(param.getTaxation()), PlatOrganization::getTaxation, param.getTaxation())
                    .eq(StringUtils.isNotBlank(param.getPiaohao()), PlatOrganization::getPiaohao, param.getPiaohao())
                    .eq(StringUtils.isNotBlank(param.getTelphone()), PlatOrganization::getTelphone, param.getTelphone())
                    .eq(StringUtils.isNotBlank(param.getMobile()), PlatOrganization::getMobile, param.getMobile())
                    .eq(StringUtils.isNotBlank(param.getFax()), PlatOrganization::getFax, param.getFax())
                    .eq(StringUtils.isNotBlank(param.getHome()), PlatOrganization::getHome, param.getHome())
                    .eq(StringUtils.isNotBlank(param.getEmail()), PlatOrganization::getEmail, param.getEmail())
                    .eq(StringUtils.isNotBlank(param.getAddress()), PlatOrganization::getAddress, param.getAddress())
                    .eq(StringUtils.isNotBlank(param.getGoodsAddress()), PlatOrganization::getGoodsAddress, param.getGoodsAddress())
                    .eq(StringUtils.isNotBlank(param.getWhouseAddress()), PlatOrganization::getWhouseAddress, param.getWhouseAddress())
                    .eq((param.getPaymentId() != null && param.getPaymentId() != 0), PlatOrganization::getPaymentId, param.getPaymentId())
                    .eq((param.getApplyType() != null), PlatOrganization::getApplyType, param.getApplyType())
                    .eq(StringUtils.isNotBlank(param.getApplyIdNumber()), PlatOrganization::getApplyIdNumber, param.getApplyIdNumber())
                    .eq(StringUtils.isNotBlank(param.getApplyName()), PlatOrganization::getApplyName, param.getApplyName())
                    .eq(StringUtils.isNotBlank(param.getApplyAddress()), PlatOrganization::getApplyAddress, param.getApplyAddress())
                    .eq(StringUtils.isNotBlank(param.getApplyIssuedby()), PlatOrganization::getApplyIssuedby, param.getApplyIssuedby())
                    .eq(StringUtils.isNotBlank(param.getApplySex()), PlatOrganization::getApplySex, param.getApplySex())
                    .eq(StringUtils.isNotBlank(param.getApplyBirthplace()), PlatOrganization::getApplyBirthplace, param.getApplyBirthplace())
                    .eq(StringUtils.isNotBlank(param.getApplyBirthday()), PlatOrganization::getApplyBirthday, param.getApplyBirthday())
                    .eq(StringUtils.isNotBlank(param.getApplyIdFront()), PlatOrganization::getApplyIdFront, param.getApplyIdFront())
                    .eq(StringUtils.isNotBlank(param.getApplyIdBack()), PlatOrganization::getApplyIdBack, param.getApplyIdBack())
                    .eq((param.getChargeTelphone() != null), PlatOrganization::getChargeTelphone, param.getChargeTelphone())
                    .eq((param.getChargeWeixin() != null), PlatOrganization::getChargeWeixin, param.getChargeWeixin())
                    .eq((param.getChargeEmail() != null), PlatOrganization::getChargeEmail, param.getChargeEmail())
                    .eq((param.getProvince() != null), PlatOrganization::getProvince, param.getProvince())
                    .eq(StringUtils.isNotBlank(param.getProvinceName()), PlatOrganization::getProvinceName, param.getProvinceName())
                    .eq((param.getCity() != null), PlatOrganization::getCity, param.getCity())
                    .eq(StringUtils.isNotBlank(param.getCityName()), PlatOrganization::getCityName, param.getCityName())
                    .eq((param.getDistrict() != null), PlatOrganization::getDistrict, param.getDistrict())
                    .eq(StringUtils.isNotBlank(param.getDistrictName()), PlatOrganization::getDistrictName, param.getDistrictName())
                    .eq((param.getStreet() != null), PlatOrganization::getStreet, param.getStreet())
                    .eq((param.getAvailableBalance() != null), PlatOrganization::getAvailableBalance, param.getAvailableBalance())
                    .eq((param.getAccountBalance() != null), PlatOrganization::getAccountBalance, param.getAccountBalance())
                    .eq((param.getFreezingAmount() != null), PlatOrganization::getFreezingAmount, param.getFreezingAmount())
                    .eq((param.getDepartmentId() != null && param.getDepartmentId() != 0), PlatOrganization::getDepartmentId, param.getDepartmentId())
                    .eq(StringUtils.isNotBlank(param.getContacts()), PlatOrganization::getContacts, param.getContacts())
                    .eq(StringUtils.isNotBlank(param.getInvitedCode()), PlatOrganization::getInvitedCode, param.getInvitedCode())
                    .eq(StringUtils.isNotBlank(param.getWeixin()), PlatOrganization::getWeixin, param.getWeixin())
                    .eq(StringUtils.isNotBlank(param.getProfile()), PlatOrganization::getProfile, param.getProfile())
                    .eq((param.getStatus() != null && param.getStatus() != 0), PlatOrganization::getStatus, param.getStatus());
            Page<PlatOrganization> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(platOrganizationService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询平台机构表异常", e);
            return Response.failed("分页查询平台机构表异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询机构表
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询机构表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(platOrganizationService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增机构(供应商)")
    public Response saveRegisterAndCoding(@RequestBody @Valid PlatOrganizationAddParam param) {
        PlatOrganization data;
        try {
            data = BeanUtils.covertType(param, PlatOrganization.class);
        } catch (Exception e) {
            log.error("新增机构表Bean转换异常", e);
            return Response.failed("新增机构表Bean转换异常");
        }
        return Response.data(platOrganizationService.insert(data));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改机构表
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改机构")
    public Response update(@RequestBody @Valid PlatOrganizationUpdateParam param) {
        PlatOrganization data;
        try {
            data = BeanUtils.covertType(param, PlatOrganization.class);
        } catch (Exception e) {
            log.error("修改机构表Bean转换异常", e);
            return Response.failed("修改机构表Bean转换异常");
        }
        return Response.data(platOrganizationService.updateById(data));
    }

    /**
     * @Author:campchen
     * @Description: 供应商审批接口 0-未审批 1-已审批 2-已终止合作
     * @Date:上午 10:26 2019/5/24
     * @Param: id, sataus
     * @return: Response
     */
    @PutMapping("/platApproval")
    @ApiOperation(value = "平台供应商审批")
    public Response platApproval(Long id, Integer status) {
        return Response.data(platOrganizationService.updateById(id, status));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @GetMapping("/ids")
    @ApiOperation(value = "根据id集合查询机构")
    @ApiParam(name = "ids", value = "主键ID组", required = true)
    public Response queryById(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("查询平台机构id集合不能为空");
        }
        ids.sort(null);
        return Response.data(platOrganizationService.getByIds(ids, StringUtils.join(ids, "")));
    }

}
